//
//  MPSwitchTableCell.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/12.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPSwitchTableCell.h"
#import "MPDeviceObject.h"
#import "UIImage+Common.h"

@interface MPSwitchTableCell ()
@property (nonatomic, strong)UILabel* titleLabel;
@property (nonatomic, strong)UILabel* contentLabel;
@property (nonatomic, strong)UIImageView* topSeperatorLineImageView;
@property (nonatomic, strong)UIImageView* bottomSeperatorLineImageView;
@property (nonatomic, strong)UIImageView* bgImageView;

@end

@implementation MPSwitchTableCell

+ (CGFloat)rowHeightForObject:(id)object
{
    return 44.0f;
}

//+ (CGFloat)estimateRowsHeight
//{
//    return 44.0f;
//}

+ (BOOL)shouldUseAutoLayout
{
    return NO;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;

        [self.contentView addSubview:self.topSeperatorLineImageView];
        [self.contentView addSubview:self.bgImageView];
        [self.contentView addSubview:self.bottomSeperatorLineImageView];
        [self.bgImageView addSubview:self.titleLabel];
        [self.bgImageView addSubview:self.contentLabel];
        [self.bgImageView addSubview:self.switchCtrl];
        
        [self setupConstraints];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)prepareForReuse
{
    [super prepareForReuse];
}

- (void)setupConstraints
{
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.trailing.mas_equalTo(self);
    }];
    
    [self.topSeperatorLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).with.offset(10.0f);
        make.height.mas_equalTo(@1.0f);
    }];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.topSeperatorLineImageView.mas_bottom);
    }];
    
    [self.bottomSeperatorLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.bgImageView.mas_bottom);
        make.height.mas_equalTo(@1.0f);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).with.offset(10.0f);
        make.top.mas_equalTo(self.bgImageView).with.offset(10.0f);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).with.offset(10.0f);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(10.0f);
        make.bottom.mas_equalTo(self.bgImageView).with.offset(-10.0f);
    }];
    
    [self.switchCtrl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bgImageView);
        make.trailing.mas_equalTo(self.contentView).with.offset(-10.0f);
        make.leading.mas_equalTo(self.titleLabel.mas_trailing).with.offset(10.0f);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)setObject:(id)object
{
    [super setObject:object];
    
    if (object == nil)
    {
        return ;
    }
    else
    {
        if ([object isKindOfClass:[MPDeviceObject class]]) {
            MPDeviceObject* device = (MPDeviceObject *)object;
            [self.switchCtrl setOn:device.isPushOn animated:YES];
        }
    }
}


#pragma mark - property
- (UIImageView *)topSeperatorLineImageView
{
    if (!_topSeperatorLineImageView) {
        _topSeperatorLineImageView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor]]];
    }
    return _topSeperatorLineImageView;
}

- (UIImageView *)bottomSeperatorLineImageView
{
    if (!_bottomSeperatorLineImageView) {
        _bottomSeperatorLineImageView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor]]];
    }
    return _bottomSeperatorLineImageView;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor whiteColor]]];
    }
    return _bgImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.textColor = [UIColor colorWithHexString:kContentTextColor];
        _titleLabel.font = [UIFont systemFontOfSize:kContentFontSize];
        _titleLabel.text = @"新标通知";
    }
    return _titleLabel;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _contentLabel.textColor = [UIColor colorWithHexString:kTextUnSelectedGrayColor];
        _contentLabel.font = [UIFont systemFontOfSize:kDescribeFontSize];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.text = @"当发现您感兴趣的新标，会通过系统通知提醒您";
    }
    return _contentLabel;
}

- (MPCellSwitch *)switchCtrl
{
    if (!_switchCtrl) {
        _switchCtrl = [[MPCellSwitch alloc] init];
    }
    return _switchCtrl;
}


@end
